package com.aaa.controller;

import com.aaa.entity.Pager;
import com.aaa.entity.PaikemingxiEntity;
import com.aaa.entity.SysRoleEntity;
import com.aaa.service.*;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "PaikeServlet",urlPatterns = "/paike.action")
public class PaikeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null)
        {
            switch (method)
            {
                case "query":
                    query(request,response);
                    break;
                case "add":
                    add(request,response);
                    break;
                case "bindclass":
                    bindclass(request,response);
                    break;
                case "bindteacher":
                    bindteacher(request,response);
                    break;
                case "bindroom":
                    bindroom(request,response);
                    break;
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PaikeService service=new PaikeServiceImpl();
        List res=service.query();
        response.getWriter().print(JSON.toJSONString(res));
    }
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//
        String classid=request.getParameter("classid");
        String shiduan=request.getParameter("shiduan");
        String keshi=request.getParameter("keshi");
        String classroom=request.getParameter("classroom");
        String teacher=request.getParameter("teacher");
        String dayweek=request.getParameter("dayweek");

        PaikeService service=new PaikeServiceImpl();
        PaikemingxiEntity info=new PaikemingxiEntity(Integer.valueOf(classid),shiduan,keshi,classroom,teacher,dayweek);
        int res= service.add(info);
        PrintWriter writer = response.getWriter();
        writer.print(JSON.toJSONString(res));
    }
    private void bindclass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PaikeService service=new PaikeServiceImpl();
        List list=service.queryclass();
        response.getWriter().print(JSON.toJSONString(list));
    }
    private void bindteacher(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PaikeService service=new PaikeServiceImpl();
        List list=service.queryteacher();
        response.getWriter().print(JSON.toJSONString(list));
    }
    private void bindroom(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PaikeService service=new PaikeServiceImpl();
        List list=service.queryroom();
        response.getWriter().print(JSON.toJSONString(list));
    }
}
